
import 'package:flustars/flustars.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'common_utils.dart';
import 'log_utils.dart';

_PermissionUtil permissionUtil = _PermissionUtil._();

class _PermissionUtil {
  var requesting = false;
  var hadRequested = false;



  _PermissionUtil._();

  void requestPermission(Permission permission,
      {String? tipMsg,
      RequestSuccessFun? requestSuccessFun,
      RequestStatusFun? requestStatusFun,
      RequestFailFun? requestFailFun}) async {

    MyLog.e("msg---requesting-----requesting---"+requesting.toString());
    if (requesting) {
      return;
    }
    requesting = true;

    hadRequested = SpUtil.getBool(permission.toString(), defValue: false) ?? false;

    if (defaultTargetPlatform == TargetPlatform.iOS) {
      MyLog.e("msg---permission-----permission---"+permission.toString());
      _requestIosPermission(permission,
          tipMsg: tipMsg,
          requestStatusFun: requestStatusFun,
          requestSuccessFun: requestSuccessFun,
          requestFailFun: requestFailFun);
    } else {
      MyLog.e("msg---permission--_requestAndPermission---permission---"+permission.toString());
      _requestAndPermission(permission,
          tipMsg: tipMsg,
          requestStatusFun: requestStatusFun,
          requestSuccessFun: requestSuccessFun,
          requestFailFun: requestFailFun);
    }
  }

  void _requestIosPermission(Permission permission,
      {String? tipMsg,
        RequestSuccessFun? requestSuccessFun,
        RequestStatusFun? requestStatusFun,
        RequestFailFun? requestFailFun}) async {

    PermissionStatus status = await permission.status;
    var bool = await status.isDenied;
    LogUtil.e("bool = ${bool.toString()}");
    requestStatusFun?.call(status);
    LogUtil.e("PermissionStatus = ${status.toString()}");
    if (status == PermissionStatus.granted) {
      requestSuccessFun?.call();
      requesting = false;
    } else if (status == PermissionStatus.permanentlyDenied) {
      if (!TextUtil.isEmpty(tipMsg)) {
        CommonUtils.showGeneralDialog(tipMsg!, () {
          requestFailFun?.call();
          requesting = false;
        }, () {
          openAppSettings();
          requesting = false;
        });
      }
    } else if (status == PermissionStatus.denied) {
      permission.request().then((value) {
        MyLog.e("msg-----1111-----"+value.toString());
        if (value.isGranted) {
          requestSuccessFun?.call();
          requesting = false;
        } else {
          openAppSettings();
          requestFailFun?.call();
          requesting = false;
        }
      });
    } else {
      permission.request().then((value) {
        MyLog.e("msg-----222-----"+value.toString());
        if (value.isGranted) {
          requestSuccessFun?.call();
          requesting = false;
        } else {
          requestFailFun?.call();
          requesting = false;
        }
      });
    }
  }


  void _requestAndPermission(Permission permission,
      {String? tipMsg,
      RequestSuccessFun? requestSuccessFun,
      RequestStatusFun? requestStatusFun,
      RequestFailFun? requestFailFun}) async {
    PermissionStatus status = await permission.status;
    requestStatusFun?.call(status);
    LogUtil.e("PermissionStatus = ${status.toString()}");
    if (status == PermissionStatus.granted) {
      requestSuccessFun?.call();
      requesting = false;
      if (!hadRequested) {
        SpUtil.putBool(permission.toString(), true);
        hadRequested = true;
      }
    } else if (status == PermissionStatus.permanentlyDenied || status == PermissionStatus.denied) {
      MyLog.e("------hadRequested----------"+hadRequested.toString());
      if (status == PermissionStatus.denied && !hadRequested) {
        _requestPermission(tipMsg,permission, requestSuccessFun, requestFailFun);
        return;
      }
      MyLog.e("------tipMsg----------"+tipMsg.toString());
      Future.delayed(Duration.zero).then((value) => CommonUtils.showGeneralDialog(tipMsg!, () {}, () {
        openAppSettings();
        requesting = false;
      }));
    } else {
      _requestPermission(tipMsg,permission, requestSuccessFun, requestFailFun);
    }
  }

  void _requestPermission(String? tip,Permission permission, RequestSuccessFun? requestSuccessFun, RequestFailFun? requestFailFun) {
    MyLog.e("------hadRequested----permission------"+permission.toString());
    permission.request().then((value) {
      MyLog.e("------hadRequested----permission--11111----"+value.toString());
      SpUtil.putBool(permission.toString(), true);
      hadRequested = true;
      if (value.isGranted) {
        requestSuccessFun?.call();
      } else {
        requestFailFun?.call();

        Future.delayed(Duration.zero).then((value) => CommonUtils.showGeneralDialog(tip??"", () {}, () {
          openAppSettings();
          requesting = false;
        }));
      }
      requesting = false;
    });
  }
}

typedef RequestSuccessFun = void Function();
typedef RequestFailFun = void Function();
typedef RequestStatusFun = void Function(PermissionStatus status);
